package inventory.controler;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import utilites.ViewUtilites;

import dto.Gin;
import dto.IssueSlip;
import dto.MaterialMaster;
import dto.MaterialSpecification;

import inventory.daoImplementer.GinImplementor;
import inventory.daoImplementer.GinItemsImplementor;
import inventory.daoImplementer.IssueSlipItemImplementor;
import inventory.daoImplementer.MaterialMasterImplementor;
import inventory.daoImplementer.MaterialSpecificationImplementor;
import inventory.daoInterface.GinDao;
import inventory.daoInterface.GinItemsDao;
import inventory.daoInterface.IssueSlipItemDao;
import inventory.daoInterface.MaterialMasterDao;
import inventory.daoInterface.MaterialSpecificationDao;

import inventory.model.GinItems;
import inventory.model.IssueSlipItem;
import inventory.model.Material;
import inventory.view.Outward;

public class IssueSlipController implements ListSelectionListener
{
	private IssueSlip model;
	private Outward view;
	
	public IssueSlipController(IssueSlip model, Outward view) 
	{
		super();
		this.model = model;
		this.view = view;
		view.addlistlistener(this);
	}

	public IssueSlip getModel() 
	{
		return model;
	}

	public void setModel(IssueSlip model) {
		this.model = model;
	}

	public Outward getView() {
		return view;
	}

	public void setView(Outward view) {
		this.view = view;
	}
	
	private void setListSlip()
	{
		IssueSlipItemDao imp=new IssueSlipItemImplementor();
		IssueSlipItem[] sliparray=imp.retreiveArray("status", 1+"");
		String[] items=new String[sliparray.length];
		for(int i=0;i<items.length;i++)
			items[i]=sliparray[i].getSlipNo()+"";
		ViewUtilites.setList(view.getList1(), items);
	}
	
	public void valueChanged1(ListSelectionEvent arg0) 
	{
		 if (arg0.getValueIsAdjusting())
			 ;
		 String selection= view.getList1().getSelectedValue().toString();	
	}

	private void setIsuueSlipItemTable(String selection)
	{
		IssueSlipItemDao items=new IssueSlipItemImplementor();
		
		IssueSlipItem[]issueItems=items.retreiveArray("slipNo",selection);
		String[] data=new String[issueItems.length];
		for(int i=0;i<data.length;i++)
		{
			data[i]=issueItems[i].getmCode();
			
		}
		
		for(int k=0;k<data.length;k++)
		{
			MaterialMasterDao imp=new MaterialMasterImplementor();
			MaterialMaster mastere=imp.retreive("mCode",data[k]);
			MaterialSpecificationDao impSpec=new MaterialSpecificationImplementor();
			MaterialSpecification specs=impSpec.retreive("mCode", data[k]);
			Material materials=new Material(mastere,specs);
			issueItems[k].setMaterial(materials);
		}
		
		
		JTable temp=view.getTable();
		int sr=0;
		DefaultTableModel dm=(DefaultTableModel) temp.getModel();
		for(IssueSlipItem gin:issueItems)
		{
			dm.addRow(new Object[]{sr,gin.getMaterial().getMname(),gin.getMaterial().getSpecs(),gin.getMaterial().getUnit(),gin.getRequiredQuant(),gin.getIssueQuant()});
		}
	}
	
	 
	
	@Override
	public void valueChanged(ListSelectionEvent arg0) 
	{
	
		
	}
}
